home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / pctv4n_1.zip / XQLDICT.C < prev    next >
Text File  |  1993-06-11  |  8KB  |  345 lines

  1. /**************************************************
  2. * FILE NAME:  XQLdict.c     TITLE: dictionary listing
  3. *
  4. * AUTHOR:  K. E. North II, Resource Group, Inc.
  5. *
  6. * SYNOPSIS:
  7. *    List record structure info for Netware SQL 
  8. *    /  XQL tables
  9. *            
  10. ***************************************************
  11. *    NOTE - requires XQLMINTF interface module
  12. ***************************************************
  13. *
  14. *    initialize (strings, counts, error_status)
  15. *    assign variables to table, view
  16. *
  17. *    initiate data base connection -
  18. *         dbConnect() to login, get cursor 
  19. *    execute SQL
  20. *         dbRequest() - compile SQL statement
  21. *
  22. *    IF status <> 0 then
  23. *        while <> end of data:
  24. *             get dictionary info (dbDescribe)
  25. *            format and list columns
  26. *    ELSE
  27. *        process system error message 
  28. *        ENDIF
  29. *    terminate data base connection: dbClose()
  30. *
  31. ***************************************************/
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <conio.h>
  36. #include <string.h>
  37.  
  38. #include "xqlintf.h"
  39. #include "xqldefs.h"
  40. #include "xqlvarbl.h"
  41.  
  42. #ifndef __XQLFUNCS_H
  43. #include "xqlfuncs.h"
  44. #endif
  45.  
  46. #ifndef __DBTYPES_H
  47. #include "dbtypes.h"
  48. #endif
  49.  
  50. #ifndef __REQUEST_H
  51. #include "request.h"
  52. #endif
  53.  
  54. #ifndef __XCOLINFO_H
  55. #include "xcolinfo.h"
  56. #endif
  57.  
  58.  
  59. #undef DEBUG
  60. #undef DEBUG1
  61. #define RCOUNT        10
  62. #define DISPLAYLEN    20
  63.  
  64.  
  65. int        SQLCODE;        /* XQL status code    */
  66.  
  67. int        ExitStatus;
  68. int        StatusReturned;
  69.  
  70. void GetArgs(int, char *, char *, char *, char *);
  71.  
  72. static char    DBDictPath[80];
  73. static char    DBTable[32];
  74.  
  75. char    *table;
  76. char    *dict;
  77.  
  78. COLUMNS ColInfo[MAXFIELD];
  79. COLUMNS *fi;
  80.  
  81. struct ConnectionInfo *si;
  82. struct ViewInfo *vu;
  83. struct SQLRequest *rq;
  84.  
  85. void main (int argc, char *argv[])
  86. {
  87.  
  88. int    flen;        /* string length of field name */
  89. int i;                /* blank fill loop index   */
  90. int TotalLength;     /* sum of field lengths */
  91.  
  92. static char    SQLString[MAXSQL];
  93. static char    DictColName[DISPLAYLEN+1];
  94. char        *ColDataType;
  95.  
  96.  
  97.     si = calloc(1, sizeof(struct ConnectionInfo));       
  98.     vu = calloc(1, sizeof(struct ViewInfo));
  99.     rq = calloc(1, sizeof(struct SQLRequest));
  100.  
  101.                         /* initialize strings */
  102.     memset(SQLString,'\x0',sizeof(SQLString));
  103.     memset(DBDictPath,'\x0',sizeof(DBDictPath));
  104.     memset(DBTable,'\x0',sizeof(DBTable));
  105.  
  106.     table     = &DBTable[0];
  107.     dict     = &DBDictPath[0];
  108.  
  109.     SQLCODE = SQL_SUCCESS;    /* zero XQLM SQLCODE */
  110.     StatusReturned = SQL_SUCCESS;
  111.  
  112.     GetArgs(argc,argv[1],argv[2],table,dict);
  113.  
  114.  
  115.     /*******************************************/
  116.     /* open data base - login, get 1st cursor  */
  117.     /*******************************************/
  118.  
  119.     strcpy (si->data_path, DBDictPath);
  120.     strcpy (si->dict_path, DBDictPath);
  121.  
  122.     StatusReturned = dbConnect (si);
  123.     if ((StatusReturned == NOXQLP))
  124.       {
  125.       printf 
  126.       ("\n<XQLdict> ERROR %d: \n",
  127.                 StatusReturned);
  128.       printf 
  129.       ("\n    Neither XQLP nor NSREQ is loaded.\n");
  130.       exit (1);
  131.       }
  132.     if ((StatusReturned == NOXQLM))
  133.       {
  134.       printf 
  135.       ("\n<XQLdict> ERROR %d: \n",
  136.                 StatusReturned);
  137.       printf 
  138.       ("\n    Neither XQLM nor NSREQ is loaded.\n");
  139.       exit (1);
  140.       }
  141.     if ((StatusReturned != SQL_SUCCESS))
  142.       {
  143.       printf 
  144.         ("\n<XQLdict> ERROR %d: Unable to open %s",
  145.                         StatusReturned, DBDictPath);
  146.       exit (1);
  147.       }
  148.  
  149.                         /* specify query string */
  150.     strcpy(SQLString,"SELECT * FROM ");
  151.     strcat(SQLString,DBTable);
  152.                         /* save request string */
  153.     strcpy(rq->Statement,SQLString);
  154.  
  155.                     /************************/
  156.                     /*  compile and execute */
  157.                     /************************/
  158.     SQLCODE = SQL_SUCCESS;
  159.     StatusReturned = SQL_SUCCESS;
  160.     StatusReturned = dbRequest(rq);
  161.     if ((StatusReturned != SQL_SUCCESS))
  162.         {
  163.         if (StatusReturned < 0 )
  164.             {
  165.             printf 
  166.             ("\n<XQLdict> STATUS: compiling  %s\n",
  167.                      SQLString);
  168.             }
  169.         else
  170.             {
  171.             printf 
  172.             ("\n<XQLdict> ERROR: compiling  %s\n", 
  173.                     SQLString);
  174.             exit (1);
  175.             }
  176.         }
  177.  
  178.                         /************************/
  179.                         /*  get dictionary         */
  180.                         /************************/
  181.  
  182.     printf ("\nStructure for table: %s\n", DBTable);                
  183.     printf ("Column     Column Name            Type\
  184.     Width  Dec  Int.Size\n");
  185.  
  186.     SQLCODE = SQL_SUCCESS;
  187.     StatusReturned = SQL_SUCCESS;            
  188.  
  189.     ColPosition = 0; 
  190.     vu->ColCount = 0;
  191.     TotalLength = 0;
  192.     vu->RowLength = 2; /* column lengths + separators */
  193.  
  194.     do
  195.         {
  196.                     /* point to column descriptor */
  197.         fi = &ColInfo[ColPosition];
  198.         StatusReturned = SQL_SUCCESS;
  199.  
  200.                 /* get data dictionary for 
  201.                 the current cursor ID */
  202.         StatusReturned = 
  203.             dbDescribe( ColPosition, 
  204.                             fi, rq );
  205.  
  206.         if ((StatusReturned != SQL_SUCCESS))
  207.             {
  208.             if (StatusReturned == INVALIDPOS)
  209.               {
  210.               break;
  211.               }
  212.             else
  213.               {
  214.             printf 
  215.               ("\n<XQLdict> ERROR %d: \n",
  216.                 StatusReturned);
  217.             printf 
  218.               ("\n getting data dictionary for %s\n", 
  219.                   SQLString);
  220.               exit (1);
  221.               }
  222.             };
  223.     vu->ColCount++;        /* update column count */
  224.  
  225.                         /* re-calc row length */
  226.     vu->RowLength +=
  227.         ColInfo[ColPosition].Width + 2;
  228.  
  229.                         /* re-calc cols length */
  230.     TotalLength +=
  231.         ColInfo[ColPosition].Width;
  232.  
  233.  
  234.     ColDataType = DecodeXQLDataType(fi->DataType);
  235.     strcpy(DictColName,fi->ColName);
  236.     flen = strlen(fi->ColName);
  237.     for ( i=flen; i < DISPLAYLEN; i++ );
  238.         {
  239.         DictColName[i]='\x20';
  240.         }
  241.     DictColName[DISPLAYLEN]='\x0';
  242.     printf 
  243.     ("\n %3d  %20.20s  %12.12s   %5d  %2d  %5d", 
  244.                         vu->ColCount,
  245.                         DictColName,
  246.                         ColDataType,
  247.                         fi->Width,
  248.                         fi->Scale,
  249.                         fi->Size);
  250.  
  251.     ColPosition++;
  252.         }
  253.     while (StatusReturned == SQL_SUCCESS);
  254.  
  255.                     /* display summary */
  256.     printf 
  257.     ("\nTotal: %d      Maximum View Length: %d\n",
  258.         TotalLength, vu->RowLength);
  259.  
  260.                         /****************/
  261.                         /*    close        */
  262.                         /****************/
  263.     SQLCODE = SQL_SUCCESS;
  264.     StatusReturned = dbClose ();
  265.     if ( StatusReturned )
  266.         {
  267.         printf 
  268.           ("\n<XQLdict> ERROR %d: \n",
  269.             StatusReturned);
  270.         printf 
  271.           ("\n dbClose failed, CursorID: %d\n", 
  272.                         rq->CursorID);
  273.         exit (1);
  274.         }
  275. }
  276.  
  277. /**************************************************
  278. * FUNCTION NAME: GetArgs
  279. *
  280. * AUTHOR:         Ken North 
  281. *
  282. * SYNOPSIS:
  283. *
  284. *     get table name and dictionary path from 
  285. *    command line or user
  286. *
  287. ***************************************************/
  288.  
  289. void GetArgs(int argc, char *arg1, char *arg2, 
  290.                     char *table, char *dict)
  291. {
  292. static char name [80]; 
  293.  
  294.   if (argc > 1)
  295.     {
  296.     memset(name,'\x0',sizeof(name));
  297.     if (*arg1 == '?')
  298.       {
  299.       printf("\n  Usage: XQLDICT table \
  300.  dictionarypath\n");
  301.       printf ("\n XQLDICT    Netware SQL / XQL \
  302. data definitions\n");
  303.       printf("   (Requires Btrieve and Netware SQL \
  304. or XQL)\n\n");
  305.       printf
  306.       ("    To display a screen at a time, type:\n");
  307.       printf
  308.       ("       XQLDICT table dictionarypath | \
  309. MORE\n\n");
  310.       printf
  311.       ("    To list to a file, type:\n");
  312.       printf
  313.       ("       XQLDICT table dictionarypath > \
  314. listfile\n\n\n");
  315.       exit(1);
  316.       }            
  317.     strcpy (name, arg1);
  318.     strupr (name);  /* translate to upper case */
  319.     strcpy(table,name) ;     /* copy name to table */
  320.     memset(name,'\x0',sizeof(name));
  321.     }
  322.   if (argc > 2)
  323.     {
  324.     strcpy (name, arg2);
  325.     strupr (name);    /* translate to upper case */
  326.     strcpy(dict,name) ;
  327.     }
  328.   if (argc < 2)
  329.       {
  330.       printf ("Table name ? ");
  331.       gets (name);
  332.       puts ("\n");
  333.       strupr (name);    /* translate to upper case */
  334.       strcpy(table,name) ;
  335.       memset(name,'\x0',sizeof(name)); /* zero name */
  336.       printf ("Dictionary pathname ? ");
  337.       gets (name);
  338.       puts ("\n");
  339.       strupr (name); 
  340.       strcpy(dict,name) ;
  341.       }
  342. }
  343.  
  344.